package com.example.controller;

import com.example.entity.Household;
import com.example.entity.Staffs;
import com.example.publisher.config.SendMessageConfig;
import com.example.publisher.config.SendPhoneMessageConfig;
import com.example.result.CommonResult;
import com.example.service.LoginService;
import com.example.utils.redis.RedisUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @project: smart-property
 * @description: 登录 controller 层
 * @author: cjq
 * @create: 2022-11-14 08:50
 **/
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    LoginService loginService;

    @Autowired
    RedisUtil redisUtil;

    @Resource(name="RabbitTemplateRaliable")
    RabbitTemplate rabbitTemplate;

    /**
     * 后台 用户密码 登录
     * @param staffs
     * @return
     */
    @PostMapping("/login")
    public CommonResult login(@RequestBody Staffs staffs){
        return CommonResult.ok(loginService.login(staffs));
    }

    /**
     * 后台 新用户注册
     * @param staffs
     * @return
     */
    @PostMapping("/register")
    public CommonResult register(@RequestBody Staffs staffs){
        return CommonResult.ok(loginService.register(staffs));
    }

    /**
     * 查询账户是否有重复
     * @param staffNumber
     * @return
     */
    @GetMapping("/listStaffNumber")
    public CommonResult listStaffNumber(String staffNumber){
        return CommonResult.ok(loginService.listStaffNumber(staffNumber));
    }

    /**
     * app端账户密码登录
     * @param household
     * @return
     */
    @PostMapping("/appLogin")
    public CommonResult appLogin(@RequestBody Household household){
        return CommonResult.ok(loginService.appLogin(household));
    }

    /**
     * app端用户注册
     * @param household
     * @return
     */
    @PostMapping("/appRegister")
    public CommonResult appRegister(@RequestBody Household household){
        return CommonResult.ok(loginService.appRegister(household));
    }

    /**
     * app端账户是否重复
     * @param accountNumber
     * @return
     */
    @GetMapping("/listAccountNumber")
    public CommonResult listAccountNumber(String accountNumber){
        return CommonResult.ok(loginService.listAccountNumber(accountNumber));
    }

    /**
     * app端邮箱是否重复
     * @param email
     * @return
     */
    @GetMapping("/listEmail")
    public CommonResult listEmail(String email){
        return CommonResult.ok(loginService.listEmail(email));
    }

    /**
     * app端手机号是否重复
     * @param phone
     * @return
     */
    @GetMapping("/listPhone")
    public CommonResult listPhone(String phone){
        return CommonResult.ok(loginService.listPhone(phone));
    }

    /**
     * app端邮箱验证码发送
     * @param email
     * @param code
     * @return
     */
    @GetMapping("/sendEmail")
    public CommonResult sendEmail(String email,String code){
        redisUtil.del("code");
        try {
            String s = UUID.randomUUID().toString();
            Message message = MessageBuilder.withBody(email.getBytes()).setMessageId(s).build();
            rabbitTemplate.convertAndSend(SendMessageConfig.CJQ_SEND_MESSAGE_EXCHANGE_NAME,SendMessageConfig.CJQ_SEND_MESSAGE_ROUTING_KEY_NAME,message,new CorrelationData(email));
            return CommonResult.ok();
        } catch (Exception e){
            System.out.println(e.getMessage());
            return CommonResult.fail("系统错误，请联系管理员！");
        }
    }

    /**
     * app端邮箱登录
     * @param household
     * @return
     */
    @PostMapping("/emailLogin")
    public CommonResult emailLogin(@RequestBody Household household){
        return CommonResult.ok(loginService.emailLogin(household));
    }

    /**
     * app端手机号验证码发送
     * @param phone
     * @param code
     * @return
     */
    @GetMapping("/sendPhone")
    public CommonResult sendPhone(String phone,String code){
        redisUtil.del("code");
        try {
            String s = UUID.randomUUID().toString();
            Message message = MessageBuilder.withBody(phone.getBytes()).setMessageId(s).build();
            rabbitTemplate.convertAndSend(SendPhoneMessageConfig.CJQ_SEND_PHONE_MESSAGE_EXCHANGE_NAME,SendPhoneMessageConfig.CJQ_SEND_PHONE_MESSAGE_ROUTING_KEY_NAME,message,new CorrelationData(phone));
            return CommonResult.ok();
        } catch (Exception e){
            System.out.println(e.getMessage());
            return CommonResult.fail("系统错误，请联系管理员！");
        }
    }

    /**
     * app端手机号登录
     * @param household
     * @return
     */
    @PostMapping("/phoneLogin")
    public CommonResult phoneLogin(@RequestBody Household household){
        return CommonResult.ok(loginService.phoneLogin(household));
    }

    /**
     * 修改密码
     * @param household
     * @return
     */
    @PutMapping("/updatePassword")
    public CommonResult updatePassword(@RequestBody Household household){
        return CommonResult.ok(loginService.updatePassword(household));
    }
}
